﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Twi.COMMON.Core
{
    /*
     * CopyRight:©2014 踏影软件开发工作室（Tyingsoft/Twilight）版权所有
     * Author:xuzhihong
     * Create Date: 2014-07-02
     * Description: File辅助类
     */
    public class TwiFileHelper
    {

        /// <summary>
        /// 生成一个唯一文件名
        /// </summary>
        /// <param name="postfix">文件后缀（default: ".txt"）</param>
        /// <returns>文件名</returns>
        public static string GenFileName(string postfix = ".txt") {
            Random random = new Random();
            int intN = random.Next(100,999);
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffff") + "_" + intN + postfix ;
            return fileName;
        }


        /// <summary>
        /// 创建一个文件
        /// </summary>
        /// <param name="fileDirectory">文件目录，绝对，最后以斜杠结尾</param>
        /// <param name="fileName">文件名或文件后缀（如：.txt、abc.txt）</param>
        /// <returns></returns>
        public static FileInfo GetFileInfo(string fileDirectory, string fileName = ".txt",bool autoGenFileName=true)
        {
            //fileDirectory = TwiPathHelper.MapPath(fileDirectory);
            if (!Directory.Exists(fileDirectory))
            {
                Directory.CreateDirectory(fileDirectory);
            }
            
            if (autoGenFileName) {
                fileName = GenFileName(fileName);
            }
            FileInfo fi = new FileInfo(fileDirectory + fileName);             
            return fi;
        }

        /// <summary>
        /// 创建文件目录，如果存在则不会创建
        /// </summary>
        /// <param name="fileDirectory">绝对目录</param>
        public static void CreateDirectory(string fileDirectory)
        {
            //fileDirectory = TwiPathHelper.MapPath(fileDirectory);
            if (!Directory.Exists(fileDirectory))
            {
                Directory.CreateDirectory(fileDirectory);
            }
        }

        /// <summary>
        /// 文件备份
        /// </summary>
        /// <param name="fileFullPath">需要备份的文件名（带全路径）</param>
        /// <param name="fileSaveAsFullPath">备份后的文件名（带全路径），不传会按默认规则备份</param>
        /// <param name="isOverWrite">如果存在同名文件，是否覆盖。默认是</param>
        /// <returns>新备份的文件名</returns>
        public static string BackUp(string fileFullPath, string fileSaveAsFullPath = null,bool isOverWrite=true)
        {
            TwiPathHelper.ExistFile(fileFullPath, true, "需要备份的文件不存在");  
            if (string.IsNullOrEmpty(fileSaveAsFullPath)) //根据默认规则备份文件
            {                             
                int index = fileFullPath.LastIndexOf('.');
                string filePathPrefix = fileFullPath.Substring(0, index);
                string filePostfix = fileFullPath.Substring(index);
                fileSaveAsFullPath = string.Format("{0}_{1}{2}", filePathPrefix,TwiDateHelper.GetNowDateTime("yyyyMMddHHmmssfff"), filePostfix);                               
            }
            else {
                int index = fileSaveAsFullPath.LastIndexOf('\\');
                string fileDirectory = fileSaveAsFullPath.Substring(0, index + 1);

                //如果目录不存在，则创建目录
                TwiFileHelper.CreateDirectory(fileDirectory);                
            }
            File.Copy(fileFullPath, fileSaveAsFullPath, isOverWrite); 
            return fileSaveAsFullPath;
        }
    }
}
